package ${serviceImplUrl};

import ${entityUrl}.${entityName};
import ${entityDtoUrl}.${entityName}Dto;
import ${daoUrl}.${entityName}Dao;
import ${serviceUrl}.${entityName}Service;
import com.lyhealth.exception.MsgException;
import com.lyhealth.utils.enums.DataStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import com.lyhealth.service.LyUserService;
import com.lyhealth.dto.LyUserDto;
import com.lyhealth.entity.LyUser;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

/**   
 * ${entityComment}服务实现
 * @author ${author}
 */
@Service
public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Dao, ${entityName}> implements ${entityName}Service  {

    @Resource
    private ${entityName}Dao ${objectName}Dao;

    @Resource
    private LyUserService lyUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(${entityName}Dto ${objectName}Dto) {
        ${entityName} ${objectName} = new ${entityName}();
        BeanUtils.copyProperties(${objectName}Dto, ${objectName});

        ${objectName}.setDataStatus(DataStatusEnum.NORMAL.getCode());
        ${objectName}.setCreateTime(new Date());
        ${objectName}Dao.insert(${objectName});
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(${entityName}Dto ${objectName}Dto) {
        ${entityName} ${objectName} = new ${entityName}();
        BeanUtils.copyProperties(${objectName}Dto, ${objectName});
        ${objectName}Dao.updateById(${objectName});
    }

    @Override
    public ${entityName}Dto getById(Long id) {
        ${entityName} ${objectName} = ${objectName}Dao.selectById(id);
        if (${objectName} == null) {
            throw new MsgException("数据不存在");
        }
        ${entityName}Dto ${objectName}Dto = new ${entityName}Dto();
        BeanUtils.copyProperties(${objectName}, ${objectName}Dto);
        return ${objectName}Dto;
    }

    @Override
    public List<${entityName}Dto> listSearch(${entityName}Dto ${objectName}DtoSearch) {
        List<${entityName}Dto> ${objectName}Dtos = new ArrayList<${entityName}Dto>();

        QueryWrapper<${entityName}> queryWrapper = new QueryWrapper<>();
        buildQueryWrapper(queryWrapper, ${objectName}DtoSearch);

        List<${entityName}> ${objectName}s = ${objectName}Dao.selectList(queryWrapper);
        if(${objectName}s != null && ${objectName}s.size() > 0){
            for (${entityName} ${objectName} : ${objectName}s) {
                ${entityName}Dto ${objectName}Dto = new ${entityName}Dto();
                BeanUtils.copyProperties(${objectName}, ${objectName}Dto);
                ${objectName}Dtos.add(${objectName}Dto);
            }
        }

        return ${objectName}Dtos;
    }

    @Override
    public IPage<${entityName}Dto> pageSearch(Page<${entityName}> page, ${entityName}Dto ${objectName}DtoSearch) {
        QueryWrapper<${entityName}> queryWrapper = new QueryWrapper<>();
        buildQueryWrapper(queryWrapper, ${objectName}DtoSearch);

        List<${entityName}Dto> ${objectName}Dtos = new ArrayList<>();
        IPage pages = ${objectName}Dao.selectPage(page, queryWrapper);
        List<${entityName}> ${objectName}s = (List<${entityName}>)pages.getRecords();
        if (${objectName}s != null && ${objectName}s.size() > 0) {
            List<LyUser> _lyUsers = new ArrayList<>();
            Map<Long, LyUser> _lyUserMap = new HashMap<>();
            List<Long> _userIds = ${objectName}s.stream().map(${entityName}::getCreateId).distinct().collect(Collectors.toList());
            QueryWrapper<LyUser> _userWrapper = new QueryWrapper<LyUser>();
            _userWrapper.in("id", _userIds);
            _lyUsers = lyUserService.list(_userWrapper);

            if (_lyUsers != null && _lyUsers.size() > 0) {
                _lyUserMap = _lyUsers.stream().collect(Collectors.toMap(LyUser::getId, e -> e));
            }

            for (${entityName} ${objectName} : ${objectName}s) {
                ${entityName}Dto ${objectName}Dto = new ${entityName}Dto();
                BeanUtils.copyProperties(${objectName}, ${objectName}Dto);

                LyUser _lyUser = _lyUserMap.get(${objectName}Dto.getCreateId());
                if (_lyUser != null) {
                    LyUserDto _lyUserDto = new LyUserDto();
                    BeanUtils.copyProperties(_lyUser, _lyUserDto);
                    ${objectName}Dto.setLyUserDto(_lyUserDto);
                }

                ${objectName}Dtos.add(${objectName}Dto);
            }
            pages.setRecords(${objectName}Dtos);
        }
    return pages;
    }

    @Override
    public Long countSearch(${entityName}Dto ${objectName}DtoSearch) {
        QueryWrapper<${entityName}> queryWrapper = new QueryWrapper<${entityName}>();
        buildQueryWrapper(queryWrapper, ${objectName}DtoSearch);

        return ${objectName}Dao.selectCount(queryWrapper);
    }

    /**
    * 组装扩展查询条件
    * @param queryWrapper
    * @param ${objectName}DtoSearch
    */
    private void buildQueryWrapper(QueryWrapper<${entityName}> queryWrapper, ${entityName}Dto ${objectName}DtoSearch){
        queryWrapper.eq("data_status", DataStatusEnum.NORMAL.getCode());
        //Todo

        queryWrapper.setEntity(${objectName}DtoSearch);
    }

}